package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.R;
import com.itheima.constant.TokenConstant;
import com.itheima.domain.User;
import com.itheima.domain.vo.UserVo;
import com.itheima.mapper.UserMapper;
import com.itheima.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    public R<UserVo> clientUserLogin( Map map){

        //接收前端用户输入的手机号和验证码
        String phone = (String) map.get("phone");
        String codes = (String) map.get("code");


        //从redis数据库中获取验证码和手机号
        String phone1 = (String)redisTemplate.opsForValue().get("phone");
        String code1 = (String)redisTemplate.opsForValue().get("code");

        LambdaQueryWrapper<User> lqw=new LambdaQueryWrapper<>();
        lqw.eq(User::getPhone,phone1);
        User user = getOne(lqw);


        String tokenString=""+System.currentTimeMillis()+phone;
        //加密
        String token = DigestUtils.md5DigestAsHex(tokenString.getBytes(StandardCharsets.UTF_8));
        //往redis数据库中添加键值对并设置存在时间
        redisTemplate.opsForValue().set(TokenConstant.USER_TOKEN_PREFIX+token, JSON.toJSONString(user),300, TimeUnit.MINUTES);

        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user,userVo);
        userVo.setToken(token);



        boolean result1 = codes.equals(code1);
        boolean result2 = phone.equals(phone1);

        if (result1&&result2){
            return R.success(userVo);
        }

        return R.error("登陆失败");



    }


    public R<String> clientSendmessage(User user){


        String phone = user.getPhone();

        LambdaQueryWrapper<User> lqw=new LambdaQueryWrapper<>();
        lqw.eq(User::getPhone,phone);
        User usertemp = getOne(lqw);
        if (usertemp==null){
           save(user);
        }

        //模拟短信发送
        Random random = new Random();
        int i = random.nextInt(8999)+1000;
        String code=""+i;
        System.out.println("验证码"+code);

        //把用户输入的手机号和系统生成的验证码存入redis数据库中
        redisTemplate.opsForValue().set("phone",phone,300,TimeUnit.SECONDS);
        redisTemplate.opsForValue().set("code",code,300,TimeUnit.SECONDS);
        return R.success("短信发送成功");
    }

    public R<String> clientLoginOut( String token) {
        // 删除数据库中的值通过key
        redisTemplate.delete(TokenConstant.USER_TOKEN_PREFIX + token);
        redisTemplate.delete("phone");
        redisTemplate.delete("code");
        return R.success("退出成功");
    }
}
